package com.iplant.mes.serviceImpl.dao.ipt;

import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.po.ipt.IPTValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DBBaseDAO(IPTValue.class)
@Slf4j
public class IPTValueDAO extends BaseDAO {

    private static IPTValueDAO Instance = null;

    public static IPTValueDAO getInstance() {
        if (Instance == null) {
            Instance = new IPTValueDAO();
        }
        return Instance;
    }

    public IPTValueDAO() {
        super("");
        this.Init();
    }

    /**
     * 批量更新或新增数据
     *
     * @param items 要更新或新增的Item对象列表
     */
    public void batchUpdateOrInsert(BMSEmployee wLoginUser, List<IPTValue> items) {
        try {
            List<Map<String, Object>> parameterSources = new ArrayList<>();
            String sql = "";

            for (IPTValue item : items) {
                Map<String, Object> params = new HashMap<>();
                params.put("ID", item.getID());
                params.put("RoutePartPointID", item.getRoutePartPointID());
                params.put("RouteStepID", item.getRouteStepID());
                params.put("Value", item.getValue());
                params.put("Remark", item.getRemark());
                params.put("Result", item.getResult());
                params.put("TaskID", item.getTaskID());
                params.put("ImagePath", item.getImagePath());
                params.put("VideoPath", item.getVideoPath());
                params.put("Manufactor", item.getManufactor());
                params.put("Modal", item.getModal());
                params.put("Number", item.getNumber());
                params.put("Status", item.getStatus());
                params.put("DisassemblyComponents", item.getDisassemblyComponents());
                params.put("RepairParts", item.getRepairParts());
                params.put("AssemblyParts", item.getAssemblyParts());
                params.put("SRDisassemblyComponents", item.getSRDisassemblyComponents());
                params.put("SRRepairParts", item.getSRRepairParts());
                params.put("SRAssemblyParts", item.getAssemblyParts());
                params.put("SRScrapParts", item.getSRScrapParts());
                params.put("SRLYParts", item.getSRLYParts());
                params.put("MaterialID", item.getMaterialID());
                params.put("Code", item.getCode());
                params.put("CreateTime", item.getCreateTime());
                params.put("CreatorID", wLoginUser.ID);
                params.put("EditTime", item.getEditTime());
                params.put("EditorID", wLoginUser.ID);
                params.put("Description", item.getDescription());

                parameterSources.add(params);

                // 根据ID正负决定使用哪个SQL模板
                if (item.getID() > 0) {
                    sql = StringUtils.Format("UPDATE `{0}`.`ipt_value`  " +
                            "SET `ID` = :ID,  " +
                            "  `Value` = :Value,  " +
                            "  `Remark` = :Remark,  " +
                            "  `Result` = :Result,  " +
                            "  `Code` = 'Code',  " +
                            "  `EditTime` = now(),  " +
                            "  `EditorID` = :EditorID,  " +
                            "  `Description` = :Description  " +
                            "WHERE `ID` = :ID", DBSchemas.MES.getDBName());
                } else {
                    sql = StringUtils.Format("INSERT INTO `{0}`.`ipt_value` " +
                            "            (`RoutePartPointID`, " +
                            "             `RouteStepID`, " +
                            "             `Value`, " +
                            "             `Remark`, " +
                            "             `Result`, " +
                            "             `TaskID`, " +
                            "             `Manufactor`, " +
                            "             `Modal`, " +
                            "             `Number`, " +
                            "             `Status`, " +
                            "             `DisassemblyComponents`, " +
                            "             `RepairParts`, " +
                            "             `AssemblyParts`, " +
                            "             `SRDisassemblyComponents`, " +
                            "             `SRRepairParts`, " +
                            "             `SRAssemblyParts`, " +
                            "             `SRScrapParts`, " +
                            "             `SRLYParts`, " +
                            "             `MaterialID`, " +
                            "             `Code`, " +
                            "             `CreateTime`, " +
                            "             `CreatorID`, " +
                            "             `EditTime`, " +
                            "             `EditorID`, " +
                            "             `Description`) " +
                            "VALUES (:RoutePartPointID, " +
                            "        :RouteStepID, " +
                            "        :Value, " +
                            "        :Remark, " +
                            "        :Result, " +
                            "        :TaskID, " +
                            "        :Manufactor, " +
                            "        :Modal, " +
                            "        :Number, " +
                            "        :Status, " +
                            "        :DisassemblyComponents, " +
                            "        :RepairParts, " +
                            "        :AssemblyParts, " +
                            "        :SRDisassemblyComponents, " +
                            "        :SRRepairParts, " +
                            "        :SRAssemblyParts, " +
                            "        :SRScrapParts, " +
                            "        :SRLYParts, " +
                            "        :MaterialID, " +
                            "        :Code, " +
                            "        now(), " +
                            "        :CreatorID, " +
                            "        now(), " +
                            "        :EditorID, " +
                            "        :Description)", DBSchemas.MES.getDBName());
                }
            }

            if (!parameterSources.isEmpty()) {
                MapSqlParameterSource[] batch = new MapSqlParameterSource[parameterSources.size()];
                for (int i = 0; i < parameterSources.size(); i++) {
                    batch[i] = new MapSqlParameterSource(parameterSources.get(i));
                }

                this.batchUpdate(sql, batch);
            }
        } catch (Exception ex) {
            log.error("batchUpdateOrInsert", ex);
        }
    }
}
